<?php
require_once 'common.php';

/**
 * 站内消息模型
 * @author wangjiajun
 *
 */
class OaSiteNoticeModel extends OpModel
{
    // 消息是否可见
    const NOTICE_UNVISIBLE = 0;     // 不可见
    const NOTICE_VISIBLE = 1;       // 可见
    
    // 消息类型
    const NOTICE_TYPE_APP = 1;            // 产品
    const NOTICE_TYPE_OPERATION = 2;      // 运维
    const NOTICE_TYPE_SERVICE = 3;        // 服务
    
	//消息发布目标
	const TARGET_INDEX = 1;
	const TARGET_SYS_MESSAGE = 2;
	const TARGET_INDEX_AND_SYS_MESSAGE = 3;

    private $_siteNoticeTable;
    
    public static function getConstantValueByName($name, $default = 0)
    {
        return self::_getConstByName($name, '', $default, 'OaSiteNoticeModel');
    }
    
    public function __construct($logger = null)
    {
        if (is_null($logger)) {
            $logger = getOaModelLogger();
        }
        parent::__construct($logger);
        $dbConfig = Oa_Util_Config::getSingleDbConfig(OA_WEB_DB_CLUSTER_ID);
        $db = OpDbModel::getInstance($dbConfig, $logger);
        $this->_siteNoticeTable = new OpTableModel($db, 'site_notice', $logger);
    }
    
    /**
     * 新建一条消息
     * @param string $title
     * @param string $content
     * @param int $creator
     * @param int $type
     * @param array $row
     * @return int|boolean
     */
    public function createNotice($title, $content, $creator, $type, $row = array())
    {
        $row = array_merge(array(
            'title' => $title,
            'content' => $content,
            'type' => $type,
            'is_visible' => self::NOTICE_VISIBLE,
            'show_begin_time' => date('Y-m-d H:i:s'),
            'show_end_time' => date('Y-m-d H:i:s'),
            'creator' => $creator,
            'create_time' => date('Y-m-d H:i:s')
        ), $row);
        return $this->_siteNoticeTable->insert($row);
    }
    
    /**
     * 获取一条消息
     * @param int $id
     * @param array $columns
     * @return array|null|boolean
     */
    public function getNotice($id, $columns = null)
    {
        return $this->_siteNoticeTable->getRow(array(
        	'id' => $id
        ), null, $columns);
    }
    
    /**
     * 查询消息列表
     * @param array $where
     * @param int $offset
     * @param int $count
     * @param string $sort
     * @param array $otherWhere
     * @return array|boolean
     */
    public function getNoticeList($where = null, $offset = 0, $count = null, 
        $sort = null, $otherWhere = null)
    {
        return $this->_siteNoticeTable->get($where, $offset, $count, $sort, $otherWhere);
    }
    
    /**
     * 查询消息个数
     * @param array $where
     * @param array $otherWhere
     * @return int|boolean
     */
    public function getNoticeCount($where = null, $otherWhere = null)
    {
        $sql = "SELECT COUNT(*) FROM site_notice" . getSqlCond($where, null, null, null, $otherWhere);
        $row = $this->_siteNoticeTable->query($sql, PDO::FETCH_NUM);
        if ($row === false) {
            $this->_logger->log("query failed");
            return false;
        }
        return (int) $row[0][0];
    }
    
    /**
     * 修改消息
     * @param int $id
     * @param array $row
     * @return boolean
     */
    public function modifyNotice($id, $row)
    {
        return $this->_siteNoticeTable->update(array(
            'id' => $id
        ), $row);
    }
    
    /**
     * 删除消息
     * @param int $id
     * @return boolean
     */
    public function deleteNotice($id)
    {
        return $this->_siteNoticeTable->delete(array(
            'id' => $id
        ));
    }
}